﻿function PAGE_LOAD() {
    try
    {
        //類別下拉指定
        if ($("#MainContent_hidMODE").val() != "Insert") {
            var DEPT_ID = $("#MainContent_ddldept option:selected").val();
            getCategory(DEPT_ID, $("#MainContent_hidcategory").val());
            getDocument(DEPT_ID, $("#MainContent_hidcategory").val(), $("#MainContent_hiddocument").val());
            $("#MainContent_ddldept").attr("disabled", "true");
            $("#ddl_category").attr("disabled", "true");
            $("#ddl_document").attr("disabled", "true");
        }
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "Maintain_Save", "USER", "js", err);
        return false;
    }
}
function check() {
    try
    {
    var message = "";
    var DEPT_NO = $("#MainContent_ddldept option:selected").val();
    var CATEGORY_ID = $("#ddl_category option:selected").val();
    var DOCUMENT_NO = $("#ddl_document option:selected").val();
    if (DEPT_NO == "") {
        message = "請選擇部門代號!\n"
    }
    if (CATEGORY_ID == "") {
        message = message + "請選擇表單類別代號!\n"
    }
    if (DOCUMENT_NO == "") {
        message = message + "請選擇表單代號!\n"
    }
    if ($("#MainContent_hl_filepath")[0].innerHTML == "" && $("#MainContent_FileUploadDoc").val() == "") {
        message = message + "請選擇表單上傳!\n"
    }

    if (message.length > 0) {
        alert(message);
        return false;
    }

    if ($("#MainContent_hidMODE").val() == "Insert") {
        //檢核資料是否已存在
        $.ajax({
            type: "POST",
            async:false,
            url: "krj_document_detail.aspx/getDATA",
            data: "{'DEPT_ID' : '" + DEPT_NO + "','CATEGORY_ID' : '" + CATEGORY_ID + "','DOCUMENT_NO' : '" + DOCUMENT_NO + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xmlHttpRequest, error) {
                var m_ErrMessage = xmlHttpRequest.responseText;
                alert(m_ErrMessage);
            },
            success: function (data) {
                if (data != null) {
                    if (data.d.indexOf("error") >= 0) {
                        //alert(data.d.replace("error", ""));
                        message = data.d.replace("error", "");
                        //return false;
                    }
                    
                }

            }

        });
    }
    
    if (message.length > 0) {
        alert(message);
        return false;
    }
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "Maintain_Save", "USER", "js", err);
        return false;
    }
   
}

function getCategory(DEPT_NO, Category) {
    try
    {
    $.ajax({
        type: "POST",
        url: "krj_document_detail.aspx/getCATEGORY",
        data: "{'DEPT_ID' : '" + DEPT_NO + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xmlHttpRequest, error) {
            var m_ErrMessage = xmlHttpRequest.responseText;
            alert(m_ErrMessage);
        },
        success: function (data) {
            if (data != null) {
                if (data.d.indexOf("error") >= 0) {
                    if (data.d.indexOf("error_no") < 0) {
                        alert(data.d.replace("error", ""));
                    }

                }
                else {
                    $.each(data.d, function (RowCount, DataRow) {
                        $("#ddl_category").append($("<option></option>").val(DataRow.DOCUMENT_CATEGORY).text(DataRow.DOCUMENT_CATEGORY_NAME));
                        $("#ddl_category option[value=" + Category + "]").attr("selected", true);

                    });

                }
            }

        }

    });
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "Maintain_Save", "USER", "js", err);
        return false;
    }
}
function getDocument(DEPT_NO, CATEGORY_ID, DOCUMENT_NO) {
    try
    {
    $.ajax({
        type: "POST",
        url: "krj_document_detail.aspx/getDOCUMENT",
        data: "{'DEPT_ID' : '" + DEPT_NO + "','CATEGORY_ID' : '" + CATEGORY_ID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xmlHttpRequest, error) {
            var m_ErrMessage = xmlHttpRequest.responseText;
            alert(m_ErrMessage);
        },
        success: function (data) {
            if (data != null) {
                if (data.d.indexOf("error") >= 0) {
                    if (data.d.indexOf("error_no") < 0) {
                        alert(data.d.replace("error", ""));
                    }

                }
                else {
                    $.each(data.d, function (RowCount, DataRow) {
                        $("#ddl_document").append($("<option></option>").val(DataRow.DOCUMENT_NO).text(DataRow.DOCUMENT_NAME));
                        $("#ddl_document option[value=" + DOCUMENT_NO + "]").attr("selected", true);

                    });

                }
            }

        }

    });
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "Maintain_Save", "USER", "js", err);
        return false;
    }
}



$(document).ready(function () {
    $("#MainContent_ddldept").change(function () { //事件發生
        //清空
        $("#ddl_category").empty();
        $("#ddl_document").empty();
        $("#ddl_category").append($("<option></option>").val("").text(""));
        $("#ddl_document").append($("<option></option>").val("").text(""));

        getCategory($(this).val(),"")
         
    });

    $("#ddl_category").change(function () { //事件發生
        //清空
        $("#ddl_document").empty();
        $("#ddl_document").append($("<option></option>").val("").text(""));
        $("#MainContent_hidcategory").val($(this).val());
        
        var DEPT_ID = $("#MainContent_ddldept").val();
        getDocument(DEPT_ID, $(this).val(), "");
        
    });

    $("#ddl_document").change(function () { //事件發生
        $("#MainContent_hiddocument").val($(this).val());
        $("#MainContent_hiddocument_name").val($.trim($('#ddl_document option:selected').text().replace($(this).val(), "")));
        
       
    });
});